package cn.org.july.weixin.service.message;

import cn.org.july.weixin.common.bean.message.TextMessage;
import cn.org.july.weixin.common.constant.MessageType;
import cn.org.july.weixin.common.constant.WxConstant;
import cn.org.july.weixin.common.interfaces.IMessage;
import cn.org.july.weixin.common.interfaces.IMessageData;
import cn.org.july.weixin.common.utils.HttpClientUtils;
import cn.org.july.weixin.service.config.WxConfig;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.Map;

/**
 * @auther: wanghongjie
 * @blame: wanghongjie
 * @date: 2020-08-30 20:01
 * @Description:
 */
public class TextMessageService implements IMessage {

    private final WxConfig config;

    public TextMessageService(WxConfig config) {
        this.config = config;
    }

    @Override
    public String handler(Map<String, Object> map, IMessageData messageData) {
        String value = (String) map.get(WxConstant.CONTENT);
        String url = config.getHttpUrl().concat("message/").concat(MessageType.TEXT.name()).concat("/").concat(value);
        String content = "欢迎使用公众号,您的问题暂时无法帮您解答，请您先留言，稍后由专人为您解答您的疑问。";
        try {
            String s = HttpClientUtils.get(url);
            JSONObject jsonObject = JSONObject.parseObject(s);
            content = jsonObject.getString("data");
        } catch (IOException e) {
        }
        TextMessage of = TextMessage.ofSendMsg(map, content);
        return of.toXml();
    }


}
